<template>
  <div class="xtx-form">
    <div class="user-info">
      <img v-if="avatar" :src="avatar" alt="" />
    <img v-else src="@/assets/images/load.gif" alt="">
      <p>Hi，{{nickname}} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input class="input" type="text" v-model="form.mobile" placeholder="绑定的手机号" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input class="input" type="text"  v-model="form.code" placeholder="短信验证码" />
        <span class="code" @click="sendCode()">发送验证码</span>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit()">立即绑定</a>
  </div>
</template>

<script>
import QC from 'qc'
import { reactive, ref } from 'vue-demi'
import { message } from '@/components/Message'
import { reqUserQQBindCode, reqUserQQBindLogin } from '@/api/login'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  props: {
    unionId: {
      type: String
    }
  },
  name: 'CallbackBind',
  setup (props) {
    const store = useStore()
    const router = useRouter()
    const form = reactive({
      mobile: '',
      code: ''
    })
    const avatar = ref('')
    const nickname = ref('')
    if (QC.Login.check()) {
      QC.api('get_user_info').success((res) => {
        console.log(res)
        avatar.value = res.data.figureurl_1
        nickname.value = res.data.nickname
      })
    }
    const sendCode = async () => {
      // 先校验手机号
      if (!/^1[3-9]\d{9}$/.test(form.mobile)) message({ text: '手机号码格式不正确' })
      const { result } = await reqUserQQBindCode(form.mobile)
      console.log('校验成功', result)
      message({ type: 'success', text: '发送验证码成功' })
    }
    // 立即绑定
    const submit = async () => {
      console.log('qqid', props.unionId)
      const { result } = await reqUserQQBindLogin({
        unionId: props.unionId,
        ...form
      })

      store.commit('user/setprofile', result)
      router.push('/')
      console.log(result)
    }

    return { avatar, nickname, sendCode, form, submit }
  }
}
</script>

<style scoped lang='less'>
.user-info {
    width: 320px;
    height: 70px;
    margin: 0 auto;
    display: flex;
    background: #f2f2f2;
    align-items: center;
    padding: 0 10px;
    margin-bottom: 25px;
    img {
      background: #f2f2f2;
      width: 50px;
      height: 50px;
    }
    p {
      padding-left: 10px;
    }
  }
  .code {
    position: absolute;
    right: 0;
    top: 0;
    line-height: 50px;
    width: 80px;
    color: #999;
    &:hover {
      cursor: pointer;
    }
  }
</style>
